package com.gxuwz.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxuwz.core.model.Cols;
import com.gxuwz.core.model.Page;
import com.gxuwz.core.util.MainUtil;
import com.gxuwz.core.web.action.BaseAction;
import com.gxuwz.pojo.Form;
import com.gxuwz.pojo.Topic;
import com.gxuwz.service.IDataService;
import com.gxuwz.service.IFormService;
import com.gxuwz.service.ITopicService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class TopicAction extends BaseAction implements Preparable, ModelDriven<Topic>{


	protected static final String LIST_JSP = "/WEB-INF/page/topic_list.jsp";  
	protected static final String ADD_JSP = "/WEB-INF/page/topic_add.jsp"; 
	protected static final String FILED_ADD_JSP = "/WEB-INF/page/topic_field_add.jsp"; 

	@Autowired
	private ITopicService topicService;
	@Autowired
	private IFormService formService;
	@Autowired
	private IDataService dataService;
	
	private Page page;
	private Topic topic;
	private Form form;
	private List<Topic> topics;

	private String json;

	
	
	public String toList(){
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	public String toAdd(){
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	public String toFieldAdd(){
		setForwardView(FILED_ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 查询获得list
	 */
	public void list(){
		page = new Page();
		page.setCode(0);
		topic.setIsDelete(0);
		page.setData(topicService.findByExample(topic));
		page.setCount(page.getData().size());

		reJson(page);
		return;
	}
	
	
	
	/**
	 * 保存topic列表，需先转成json格式,暂时作废
	 */
//	public void saveList(){
//		Gson gson = new Gson();
//		List<Topic> topics = gson.fromJson(json, new TypeToken<List<Topic>>(){}.getType());
//		int i = 1;
//		for(Topic tc : topics){
//			tc.setFormId(form.getFormId());
//			topicService.save(tc);
//		}
//		return;
//	}
	
	public void update(){
		Gson gson = new Gson();
		String formId = getRequest().getParameter("formId");
		form = formService.findByProperty("formId", formId).get(0);
		List<Topic> topics = gson.fromJson(json, new TypeToken<List<Topic>>(){}.getType());
		List<Topic> newTopic = new ArrayList<Topic>();
		for(Topic tc : topics){
			if(MainUtil.noNull(tc.getFormId())){
				topicService.update(tc);
			}else{
				tc.setIsDelete(0);
				tc.setFormId(formId);
				newTopic.add(topicService.save(tc));
			}
		}
		//更新数据表
		dataService.editTable("data_"+form.getId(), newTopic);
		return;
	}
	
	/**
	 * 通过模板创建
	 */
	public void addByTemplate(){
		Gson gson = new Gson();
		String formId = getRequest().getParameter("formId");
		form = formService.findByProperty("formId", formId).get(0);
		List<Topic> topics = gson.fromJson(json, new TypeToken<List<Topic>>(){}.getType());
		for(Topic tc : topics){
			tc.setId(null);
			tc.setIsDelete(0);
			tc.setFormId(formId);
			tc = topicService.save(tc);
		}
		//创建数据表
		if(form.getFormType() == 0){
			dataService.createDB("data_"+form.getId(), topics);
		}else if(form.getFormType() == 2){
			dataService.createDBT("data_"+form.getId(), topics);
		}
		return;
		
		
		
	}
	
	
	public void save(){
		if(topic.getIsDelete() == null){
			topic.setIsDelete(0);
		}
		topicService.save(topic);
	}
	
	public void del(){
		if(topic.getId() != null){
			topicService.del(topic.getId());
		}
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	@Override
	public Topic getModel() {
		// TODO Auto-generated method stub
		return topic;
	}


	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	public List<Topic> getTopics() {
		return topics;
	}

	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		topic = new Topic();
		form = new Form();
		topics = new ArrayList<Topic>();
	}
	

	
	
	
}
